package com.party.service.excellentpartybranch.impl;

import com.party.common.Page;
import com.party.mapper.ExcellentPartyBranchMapper;
import com.party.model.ExcellentPartyBranch;
import com.party.model.ExcellentPartyBranchExample;
import com.party.service.excellentpartybranch.ExcellentPartyBranchService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-9-18
 * Time: 下午10:38
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class ExcellentPartyBranchServiceImpl implements ExcellentPartyBranchService {

    @Resource
    ExcellentPartyBranchMapper excellentPartyBranchMapper;

    @Override
    public int addExcellentPartyBranch(ExcellentPartyBranch excellentPartyBranch) {
        int num = excellentPartyBranchMapper.insertSelective(excellentPartyBranch);
        return num;
    }

    @Override
    public int deleteExcellentPartyBranchByPrimaryKey(Integer id) {
        int num = excellentPartyBranchMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteExcellentPartyBranchByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += excellentPartyBranchMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public ExcellentPartyBranch getExcellentPartyBranchByPrimaryKey(Integer id) {
        ExcellentPartyBranch excellentPartyBranch = excellentPartyBranchMapper.selectByPrimaryKey(id);
        return excellentPartyBranch;
    }

    @Override
    public int updateExcellentPartyBranchByPrimaryKey(ExcellentPartyBranch excellentPartyBranch) {
        int num = excellentPartyBranchMapper.updateByPrimaryKeySelective(excellentPartyBranch);
        return num;
    }

    @Override
    public Page getExcellentPartyBranchList(ExcellentPartyBranch excellentPartyBranch, int pageSize, int pageNumber) {
        ExcellentPartyBranchExample excellentPartyBranchExample = new ExcellentPartyBranchExample();
        /**
         * 可以添加查询条件
         */
        if(excellentPartyBranch != null)
        {
            ExcellentPartyBranchExample.Criteria criteria = excellentPartyBranchExample.createCriteria();
            if(excellentPartyBranch.getTitle() != null && !"".equals(excellentPartyBranch.getTitle()))
            {
                criteria.andTitleLike("%"+excellentPartyBranch.getTitle()+"%");
            }

            if(excellentPartyBranch.getAuthor() != null && !"".equals(excellentPartyBranch.getAuthor()))
            {
                criteria.andAuthorLike("%" + excellentPartyBranch.getAuthor() + "%");
            }

        }
        excellentPartyBranchExample.setOrderByClause("time desc");

        //获取总的记录数量
        int count = excellentPartyBranchMapper.countByExample(excellentPartyBranchExample);
        //获取指定的页码数据
        excellentPartyBranchExample.setBeginNum((pageNumber - 1) * pageSize);
        excellentPartyBranchExample.setEndNum(pageSize);
        List<ExcellentPartyBranch> list = excellentPartyBranchMapper.getListPage(excellentPartyBranchExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }

    @Override
    public int addExcellentPartyBranchByUserIds(String[] userids) {
        int num=0;
        if(!"".equals(userids)&&userids!=null){
            excellentPartyBranchMapper.deleteByExample(new ExcellentPartyBranchExample());
            for (String uid : userids) {
                ExcellentPartyBranch excellentPartyBranch=new ExcellentPartyBranch();
                Integer uidInt=Integer.parseInt(uid);
                excellentPartyBranch.setLogdgeid(uidInt);
                ExcellentPartyBranchExample excellentPartyBranchExample=new ExcellentPartyBranchExample();
                ExcellentPartyBranchExample.Criteria criteria=excellentPartyBranchExample.createCriteria();
                criteria.andLogdgeidEqualTo(uidInt);
                List<ExcellentPartyBranch> list=excellentPartyBranchMapper.selectByExample(excellentPartyBranchExample);
                if(list.size()==0){
                    num += excellentPartyBranchMapper.insertSelective(excellentPartyBranch);
                }
                num +=1;
            }
        }
        return num;
    }

    @Override
    public List<ExcellentPartyBranch> getAllExcellentPartyBranch() {
        ExcellentPartyBranchExample excellentPartyBranchExample = new ExcellentPartyBranchExample();
        List<ExcellentPartyBranch> list=this.excellentPartyBranchMapper.getAllExcellentPartyBranch(excellentPartyBranchExample);
        return list;
    }

    @Override
    public List<ExcellentPartyBranch> getAllExcellentPartyBranchByCustom() {
        List<ExcellentPartyBranch> list = excellentPartyBranchMapper.getAllExcellentPartyBranchByCustom();
        return list;
    }

    public Page getListPageByCustorm(ExcellentPartyBranch excellentPartyBranch, int pageSize, int pageNumber){
        ExcellentPartyBranchExample excellentPartyBranchExample = new ExcellentPartyBranchExample();

        //获取总的记录数量
        int count = excellentPartyBranchMapper.countByExample(excellentPartyBranchExample);
        //获取指定的页码数据
        excellentPartyBranch.setBeginNum((pageNumber - 1) * pageSize);
        excellentPartyBranch.setEndNum(pageSize);
        List<ExcellentPartyBranch> list = excellentPartyBranchMapper.getListPageByCustorm(excellentPartyBranch);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }
}
